
<template>
  <el-dropdown>
    <el-button type="primary">
      {{who.username}}
    </el-button>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="centerDialogVisible = true" v-if="show === false">
            登陆
        </el-dropdown-item>
        <el-dropdown-item>
          <el-link :underline="false" href="/register" v-if="show === false">注册</el-link>
        </el-dropdown-item>
        <el-dropdown-item @click="drawer  = true; " v-if="show">
            个人信息
        </el-dropdown-item>
        <el-dropdown-item @click="quit()" v-if="show">
          退出登陆
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>

  <el-dialog v-model="centerDialogVisible" center>
    <el-tabs type="border-card">
      <el-tab-pane label="学生登陆">
        <Stu_in_Form/>
      </el-tab-pane>
      <el-tab-pane label="教师登陆">
        <Ter_in_Form/>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>

  <el-drawer v-model="drawer " title="I am the title" :with-header="false">
    <Oneself/>
  </el-drawer>
</template>

<script>
import Oneself from "@/components/Oneself";
import Ter_in_Form from "@/components/Ter_in_Form";
import Stu_in_Form from "@/components/Stu_in_Form";
import {ref} from "vue";
export default {
  name: "Undown",
  components: {Stu_in_Form, Ter_in_Form, Oneself},
  data(){
    return {
      who: {username: ""}
    }
  },
  setup() {
    return {
      centerDialogVisible: ref(false),
      drawer: ref(false),
      show: false
    }
  },
  created() {
    // 先获取是否有用户已登陆
    if (JSON.parse(sessionStorage.getItem("1")) !== null){
      // 将用户信息存入 who 中，然后将用户的昵称填充到按钮面板上
      this.who = JSON.parse(sessionStorage.getItem("1"))
      // 标识符show为true，则下拉框显示”个人信息“和”退出登录“，否则显示”登陆“和”注册“
      this.show = true
      // 若无用户登陆则显示”请登录“
    }else {
      this.show = false
      this.who.username = '请登陆'
    }

  },
  methods: {
    // 退出登陆按钮
    quit(){
      // 清空session，并刷新页面重新加载信息
      sessionStorage.removeItem("1")
      sessionStorage.removeItem("2")
      this.show = false
      this.$router.push({path: '/main'})
      this.$nextTick(() => {
        location.reload()
      });

    },
  }
}
</script>

<style scoped>

</style>